package com.yolib.sanyi;

import com.yolib.sanyi.tool.Utility;
import com.yolib.sanyi.tool.YoliBLog;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DiscoverContentActivity extends Activity implements OnClickListener, AnimationListener{
	
	private WebView 	mWebView;
	private ImageView 	mBack;
	private ImageView 	mTelBtn;
	private ImageView 	mWebBtn;
	private ImageView 	mMapBtn;
	private TextView	mTitle;
	private LinearLayout mLayout;
	
	private String 		mContent; 
	private String 		mTel; 
	private String 		mUrl; 
	private String 		mAddX;
	private String 		mAddY;
	private String 		mStr;
	
	private Animation alpha_1, alpha_2;
	private AnimationSet animationSet;
	
	private boolean isShow = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_discover_content);
		
		alpha_1 = new AlphaAnimation(0.0f, 1.0f);
		alpha_1.setDuration(1000);
		alpha_2 = new AlphaAnimation(1.0f, 0.0f);
		alpha_2.setDuration(1000);
		alpha_2.setStartOffset(5000);

		// 定義兩組透明漸變動畫,alpha_1, alpha_2
		// 兩組動畫的周期為2秒,第二組延遲2.5秒開始執行.

		animationSet = new AnimationSet(true);
		animationSet.addAnimation(alpha_1);
		animationSet.addAnimation(alpha_2);
		animationSet.setAnimationListener(this);

		
		mWebView = 	(WebView) findViewById(R.id.discover_content_WebView);
		mBack	 = 	(ImageView)  findViewById(R.id.BackImageView);
		mTitle	 =  (TextView)	findViewById(R.id.discover_content_name);
		mLayout  =  (LinearLayout) findViewById(R.id.discpver_btn_layout);
		mTelBtn	 = 	(ImageView)  findViewById(R.id.discover_tel);
		mWebBtn	 = 	(ImageView)  findViewById(R.id.discover_web);
		mMapBtn	 = 	(ImageView)  findViewById(R.id.discover_map);
		
		mBack.setOnClickListener(this);
		mTelBtn.setOnClickListener(this);
		mWebBtn.setOnClickListener(this);
		mMapBtn.setOnClickListener(this);
		
		mLayout.startAnimation(animationSet);
		isShow = true;
			
		mContent = getIntent().getStringExtra("content");
		mTel	 = getIntent().getStringExtra("tel");
		mUrl	 = getIntent().getStringExtra("url");
		mAddX	 = getIntent().getStringExtra("addX");
		mAddY	 = getIntent().getStringExtra("addY");
		
		if(getIntent().getStringExtra("title") != null) {
			mStr =  getIntent().getStringExtra("title");
			Spanned spanned = Html.fromHtml(mStr);
			mTitle.setText(spanned);
		}
			
		
		YoliBLog.e(""+mContent);

		mWebView.loadDataWithBaseURL(  
			    Utility.getAPIURL(),  
			    "<html>"  
			      + "<head>"  
			      + "<meta http-quiv=\"Content-Type\" content=\"text/html charset=utf-8\" />"  
			      + "</head>"  
			      + "<body>"  
			      + mContent  
			      + "</body>"  
			      + "</html>",  
			    "text/html",  
			    "utf-8",  
			    null);  
	}

	@Override
	public void onClick(View v) {
		switch(v.getId())
		{
		case R.id.BackImageView:
			finish();
			overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);	
			break;
		case R.id.discover_tel:
			Intent dialIntent = new Intent();
			dialIntent.setAction(Intent.ACTION_CALL);
			dialIntent.setData(Uri.parse("tel:" + mTel));
			DiscoverContentActivity.this.startActivity(dialIntent);
			overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			break;
		case R.id.discover_web:
			 Uri uri = Uri.parse(mUrl);
			 Intent urlIntent = new Intent(Intent.ACTION_VIEW, uri);
			 startActivity(urlIntent);
			 overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

			break;
		case R.id.discover_map:
			Intent mapIntent = new Intent(DiscoverContentActivity.this, StoreMapActivity.class);
			mapIntent.putExtra("addX", mAddX);
			mapIntent.putExtra("addY", mAddY);
			mapIntent.putExtra("addY", mAddY);
			mapIntent.putExtra("title", mStr);
			startActivity(mapIntent);
			overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);	
			break;
		}
	}
	
	

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if(ev.getAction() == MotionEvent.ACTION_DOWN) {
			if(isShow != true)
				mLayout.startAnimation(animationSet);
		}
		return super.dispatchTouchEvent(ev);
	}

	@Override
	public void onAnimationEnd(Animation arg0) {
		mLayout.setVisibility(View.GONE);	
		isShow = false;
	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAnimationStart(Animation animation) {
		mLayout.setVisibility(View.VISIBLE);	
		isShow = true;
	}
}
